package projet.volleystat;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EndMatchActivity extends Activity {
	protected ArrayList<Player> players;
	protected ArrayList<Integer> playersid;
	protected Integer idmatch;
	protected String filename;
	protected DBInfo dbinfo;
	protected Button saveb;
	protected Button mainb;
	protected EditText filenameinput;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_end_match);

		saveb = (Button) findViewById(R.id.savetohtmlButton);
		mainb = (Button) findViewById(R.id.returntomainButton);

		Bundle bundle = getIntent().getExtras();

		idmatch = bundle.getInt("idmatch");
		playersid = bundle.getIntegerArrayList("playersid");

		players = new ArrayList<Player>();

		saveb.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						EndMatchActivity.this);
				filenameinput = new EditText(EndMatchActivity.this);
				filenameinput.setSingleLine(true);

				builder.setMessage(R.string.enterfilename);
				builder.setView(filenameinput);

				builder.setPositiveButton(R.string.save,
						new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						filename = filenameinput.getText().toString()
								.trim();
						if (filename.length() != 0) {
							dbinfo = new DBInfo(getApplicationContext());
							dbinfo.openDb();
							String path = Environment
									.getExternalStorageDirectory()
									+ "/" + filename + ".html";
							try {
								for (int i = 0; i < playersid.size(); i++) {
									players.add(dbinfo.getDb()
											.getPlayerById(
													playersid.get(i)));
								}

								dbinfo.closeDb();
								dbinfo.loadAll();
								dbinfo.exportToHTML(path,

										new ArrayList<String>(), players,
										idmatch);
								Toast.makeText(getBaseContext(),
										"Enregistré dans : " + path,
										Toast.LENGTH_LONG).show();

							} catch (DatabaseException e) {
								Toast toast = Toast.makeText(
										EndMatchActivity.this,
										R.string.dberror,
										Toast.LENGTH_LONG);
								toast.show();
								dialog.cancel();
								dbinfo.closeDb();
								e.printStackTrace();
							} catch (IOException e) {
								Toast toast = Toast.makeText(
										EndMatchActivity.this,
										R.string.writeerror,
										Toast.LENGTH_LONG);
								toast.show();
								dialog.cancel();
								dbinfo.closeDb();
								e.printStackTrace();
							}

						} else {

							Toast toast = Toast.makeText(
									EndMatchActivity.this,
									R.string.invalidname,
									Toast.LENGTH_LONG);
							dialog.cancel();
							toast.show();
						}
					}
				});

				builder.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						dialog.cancel();
					}
				});
				AlertDialog dialog = builder.create();
				dialog.show();
			}
		});

		mainb.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});
	}

	@Override
	public void onBackPressed() {
	}
}
